package com.test.action;

import java.util.Calendar;
import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

	private String username;
	private String password;
	private String repassword;
	private int age;
	private Date birthday;
	private Date graduation;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Date getGraduation() {
		return graduation;
	}

	public void setGraduation(Date graduation) {
		this.graduation = graduation;
	}

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
public String test() throws Exception {
		System.out.println("test successful!");
		return SUCCESS;
	}
	
	@Override
	public void validate() {
		System.out.println("validate");
		if (null == username) {
			//this.addFieldError("username", "username required");
			this.addActionError("username required");
		} else if (username.length() < 6 || username.length() > 12) {
		//	this.addFieldError("username", "username must be between 6 and 12");
			this.addActionError("username must be between 6 and 12");
		}
		if (null == password) {
		//	this.addFieldError("password", "password required");
			this.addActionError("passwrod required");
		} 
		else if (password.length() < 6 || password.length() > 20) {
		//	this.addFieldError("password", "password must be between 6 and 20");
			this.addActionError("password must be between 6 and 12");
		}
		if (null == repassword) {
			//this.addFieldError("repassword", "repassword required");
			this.addActionError("repassword required");
		} 
		else if (!repassword.equals(password)) {
			this.addFieldError("repassword",
					"repassword does not equal to password");
			this.addActionError(
					"repassword does not equal to password");
		}
		if (age <= 1 || age > 120) {
			//this.addFieldError("age", "age must be between 1 and 120");
			this.addActionError("age must be between 1 and 120");
		}
		if (null == birthday) {
			//this.addFieldError("bithday", "birthday required");
			this.addActionError("birthday required");
		}
		if (null == graduation) {
			//this.addFieldError("graduation", "graduation required");
			this.addActionError( "graduation required");
		}
		if ( null != birthday && null != graduation) {
			Calendar c1= Calendar.getInstance();
			c1.setTime(birthday);
			Calendar c2= Calendar.getInstance();
			c2.setTime(graduation);
			if(!c1.before(c2)){
			//	this.addFieldError("birthday", "bithday should be before graduation");
				this.addActionError( "bithday should be before graduation");
			}		
		}
	}
}
